// pages/ShoppingCart/ShoppingCart.js
Page({
    //绑定的change
    data:{
        select_all:'',
        useId:'',
        sum:'0',
        cart:[
            {
                picture:'',
                proId:'',
                proNum:'',
                clothes_size:'',
                intro:'',
                price:'',
                checked:''
            }
        ]
    },
    jumpDelete(){
        wx.navigateTo({
          url: '/pages/delete/delete',
        })
    },
    jumpOrder(e){
        console.log(e)
        var that = this; //存放选中id的数组
        for(let i=0;i<that.data.cart.length;i++){
                if(that.data.cart[i].checked == true){
                    wx.request({
                        url: 'http://localhost:8080/cart/delete?useId='+that.data.useId+'&proId='+that.data.cart[i].pro_id,
                        method:'POST',
                        header:{
                            'content-type':'application/json'
                        },
                        success:function(res) {
                            console.log(res);
                            wx.request({
                              url: 'http://localhost:8080/order/add?userId='+that.data.cart[i].proId+'&addressId=1&proId='+that.data.useId,
                              method:'POST',
                                header:{
                                'content-type':'application/json'
                                },
                                success:function(res){
                                    console.log(res);
                                    that.queryCart();
                                }
                            })
                        },
                        fail:function() {
                            console.log("失败");
                        }
                    })
                }
            
        }
    },
    change: function (e) {
        var that = this;
        e.detail.value = that.data.proNum;
    },
    queryCart(){
        var that = this;
        var app = getApp();
        that.data.useId = app.data.useId;
        wx.request({
            url: 'http://localhost:8080/cart/search?useId='+that.data.useId,
            method:'POST',
            header:{
                'content-type':'application/json'
            },
            success:function(res) {
                console.log(res);
                that.setData({
                    cart:res.data.data
                });
                console.log(that.data.cart)
            },
            fail:function() {
                console.log("失败");
            }
        })
    },
selectone(e){
    var that = this;
    var sum = 0;   //存放选中id的数组
    for(let i=0;i<that.data.cart.length;i++){
        if(that.data.cart[i].pro_id == e.currentTarget.dataset.index.pro_id){
            that.data.cart[i].checked = (!that.data.select_all);
            if(that.data.cart[i].checked == true){
                sum += that.data.cart[i].price;
            }
        }
    }
    that.setData({
        select_all:(!that.data.select_all),
        sum:sum
    })
},
//全选与反全选
selectall: function (e) {
      var that = this;
      var sum = 0;   //存放选中id的数组
      for (let i = 0; i < that.data.cart.length; i++) {
          that.data.cart[i].checked = (!that.data.select_all);
          console.log(that.data.cart[i].checked);
          if(that.data.cart[i].checked == true){
              sum += that.data.cart[i].price;
          }
      }
      that.setData({
        cart: that.data.cart,
        select_all: (!that.data.select_all),
        sum:sum
      })
    },
    onShow(){
        this.queryCart();
    }


})
